package com.ruitu.mall_98.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.beanu.arad.Arad;
import com.beanu.arad.base.my_base.MyBaseActivity;
import com.beanu.arad.utils.ToastUtils;
import com.beanu.arad.widget.progressactivity.ProgressLayout;
import com.ruitu.mall_98.R;
import com.ruitu.mall_98_1.dao.api.APIRetrofit;
import com.ruitu.router_module.A_Rutils;
import com.ruitu.router_module.bean.EventModel;
import com.ruitu.router_module.bean.api.BaseResult;
import com.ruitu.router_module.bean.api.SubscriberImpl;
import com.ruitu.router_module.bean.app.Msg;
import com.ruitu.router_module.bean.order.SmallOrder;

import java.text.SimpleDateFormat;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

//系统消息详情页面
public class SysMsgDetailActivity extends MyBaseActivity {
    private TextView tv_content, tv_theme, tv_time;//消息内容,主题,时间
    private LinearLayout ll_check;//查看详情

    private Msg msg = new Msg();

    protected int getLayoutRes() {
        return R.layout.activity_sys_msg_detail;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        tv_content = findViewById(R.id.tv_content);
        tv_theme = findViewById(R.id.tv_theme);
        tv_time = findViewById(R.id.tv_time);
        ll_check = findViewById(R.id.ll_check);
//        UIUtils.setUnderLine(tv_content);
        setHeadTitle("消息详情");

        Bundle bundle = getIntent().getExtras();
        if (null != bundle) {
            String msgId = bundle.getString("msg_id");
            reqData(msgId);
            showProgress();
        }

        ll_check.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v == ll_check) {
            SmallOrder smallOrder = new SmallOrder();
            smallOrder.setId(msg.getOrderId());
            Bundle bundle = new Bundle();
            bundle.putSerializable("smallOrder", smallOrder);
            try {
                A_Rutils.startActivity(this, "com.ruitu.mall_98_2.order.OrderDetailsActivity", bundle);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
    }

    private void reqData(String msgId) {
        APIRetrofit.getDefault().reqMsgDetial(msgId)
                .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SubscriberImpl<BaseResult<Msg>>() {
                    @Override
                    public void onError(Throwable e) {
                        hideProgress();
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(BaseResult<Msg> baseResult) {
                        Arad.bus.post(new EventModel(18122851));
                        hideProgress();
                        if (baseResult.isSuccess()) {
                            msg = baseResult.getData();
                            setDataToView();
                        } else {
                            ToastUtils.showShort(baseResult.getMsg());
                        }
                    }
                });
    }

    private void setDataToView() {
        tv_theme.setText(msg.getTitle());
        tv_content.setText(msg.getContent());
        try {
            String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Long.parseLong(msg.getCreateTime()));
            tv_time.setText(time);
        } catch (Exception e) {
            e.printStackTrace();
            tv_time.setText("1970-01-01 08:00:00");
        }

        if (!TextUtils.isEmpty(msg.getOrderId())) {
            ll_check.setVisibility(View.VISIBLE);
        } else {
            ll_check.setVisibility(View.GONE);
        }
    }

    protected ProgressLayout findProgressLayout() {
        return null;
    }
}
